// pages/item/item.js
const app = getApp()
Page({
  onLoad: function() {

    let homePage = app.pages.get('pages/home/home')
    let itemPage = app.pages.add(this)
    
    let index = homePage.data.cardIndex
    let card = homePage.data.cardList[index]
    let picUrls = JSON.parse(card.pictureUrl)
    let realUrls = this.getRealUrl(picUrls)
    
    this.getQrCode(card.userId)

    this.setData({
      obj: card,
      imageList: realUrls
    })

  },

  /**
   * 页面的初始数据
   */
  data: {
    obj: null,
    imageList: [],
    isBlur: true,
    qrCode: null
  },

  preview: function(e) {
    let url = e.currentTarget.dataset.url
    let imgList = this.data.imageList

    wx.previewImage({
      urls: imgList,
      current: url,
      success: function() {
        console.log("preview")
      },
      fail: function(e) {
        console.log(e)
      }
    })
  },

  methods: {

  },

  clearBlur: function() {
    this.setData({
      isBlur: false
    })
  },

  getRealUrl: function(list) {
    let host = app.globalData.host
    let realUrlList = []
    for (let item of list) {
      realUrlList.push(host + "/" + item)
    }
    return realUrlList
  },

  /**
   * 获取卖家二维码
   */
  getQrCode: function(userId) {
    let host = app.globalData.host
    let token = wx.getStorageSync('token')
    wx.request({
      url: host + '/user/qrCode',
      method: "GET",
      header: {
        'authorization': token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: userId
      },
      success: (res)=> {
        if (res.data.data != null) {
          console.log(res.data.data)
          this.setData({
            qrCode: host + "/" + res.data.data
          })
        }
      }
    })
  },

  previewCode: function (e) {
    let url = this.data.qrCode
    let urls = [url]

    wx.previewImage({
      urls: urls,
      current: url,
      success: function () {
        console.log("preview")
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },


})